package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.converter.ProductDetailConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by fenglin on 2020/9/16 7:44 PM
 */
@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductDetailConverter productDetailConverter;
    @Autowired
    ProductConverter productConverter;


    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        request.requestCheck();
        ProductDetailResponse response = new ProductDetailResponse();

        Item item = itemMapper.selectByPrimaryKey(request.getId());
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());

        ProductDetailDto productDetailDto = productDetailConverter.item2PDDto(item);
        productDetailDto.setDetail(itemDesc.getItemDesc());
        String[] imageSmall = productDetailDto.getProductImageSmall();
        productDetailDto.setProductImageBig(imageSmall[0]);

        response.setProductDetailDto(productDetailDto);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        return response;
    }


    //shopping/goods?page=2&size=20&sort=-1&priceGt=0&priceLte=10000
    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        request.requestCheck();
        AllProductResponse response = new AllProductResponse();
        PageHelper.startPage(request.getPage(),request.getSize());

        //默认不按价格排序
        String sortColumn = "price";
        String order = "";

        if("1".equals(request.getSort())){
            order = "ASC";
        }else if("-1".equals(request.getSort())){
            order = "DESC";
        }

        Example example = new Example(Item.class);
        example.setOrderByClause(sortColumn + " " + order);
        Example.Criteria criteria = example.createCriteria();
        criteria.andBetween(sortColumn, request.getPriceGt(), request.getPriceLte());

        List<Item> itemList = itemMapper.selectByExample(example);
        List<ProductDto> productDtoList = productConverter.items2Dto(itemList);

        //为了获得total
        PageInfo<Item> info = new PageInfo<>(itemList);


        response.setTotal(info.getTotal());
        response.setProductDtoList(productDtoList);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        return response;
    }

    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;


    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse response = new RecommendResponse();

        Example examplePanel = new Example(Panel.class);
        Example.Criteria criteriaPanel = examplePanel.createCriteria();
        criteriaPanel.andCondition("name = ", "热门推荐");
        Panel panel = panelMapper.selectOneByExample(examplePanel);

        Example examplePanelContent = new Example(PanelContent.class);
        Example.Criteria criteriaPanelContent = examplePanelContent.createCriteria();
        criteriaPanelContent.andCondition("panel_id = ", panel.getId());
        List<PanelContent> panelContentList = panelContentMapper.selectByExample(examplePanelContent);

        //返回的数据
        PanelDto panelDto = contentConverter.panen2Dto(panel);
        List<PanelContentItemDto> panelContentItemDtoList = contentConverter.panelContents2Dto2(panelContentList);


        //从tb_item中查询list的title,price,sell_point 给 panelContentItemDtoList
        for(PanelContentItemDto dto : panelContentItemDtoList){
            Example example = new Example(Item.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andCondition("id = ", dto.getProductId());
            Item item = itemMapper.selectOneByExample(example);
            dto.setProductName(item.getTitle());
            dto.setSalePrice(item.getPrice());
            dto.setSubTitle(item.getSellPoint());
        }

        panelDto.setPanelContentItems(panelContentItemDtoList);
        //把list给PanelDto
        Set<PanelDto> panelContentItemDtos = new HashSet<>();
        panelContentItemDtos.add(panelDto);

        response.setPanelContentItemDtos(panelContentItemDtos);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        return response;
    }
}
